package com.ruoyi.user.service.impl;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.business.api.RemoteCouponService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.user.mapper.UserCouponMapper;
import com.ruoyi.user.domain.UserCoupon;
import com.ruoyi.user.service.IUserCouponService;

/**
 * 用户优惠卷中间Service业务层处理
 * 
 * @author zr
 * @date 2024-09-11
 */
@Service
public class UserCouponServiceImpl implements IUserCouponService 
{
    @Autowired
    private UserCouponMapper userCouponMapper;

    @Autowired
    private RemoteCouponService remoteCouponService;

    private static final Lock lock = new ReentrantLock();

    /**
     * 查询用户优惠卷中间
     * 
     * @param id 用户优惠卷中间主键
     * @return 用户优惠卷中间
     */
    @Override
    public UserCoupon selectUserCouponById(Long id)
    {
        return userCouponMapper.selectUserCouponById(id);
    }

    /**
     * 查询用户优惠卷中间列表
     * 
     * @param userCoupon 用户优惠卷中间
     * @return 用户优惠卷中间
     */
    @Override
    public List<UserCoupon> selectUserCouponList(UserCoupon userCoupon)
    {
        return userCouponMapper.selectUserCouponList(userCoupon);
    }

    @Override
    public List<UserCoupon> selectCouponListByUserId(UserCoupon userCoupon) {
        return userCouponMapper.selectCouponListByUserId(userCoupon);
    }

    /**
     * 新增用户优惠卷中间
     * 
     * @param userCoupon 用户优惠卷中间
     * @return 结果
     */
    @Override
    public synchronized int insertUserCoupon(UserCoupon userCoupon)
    {
        Long cId = userCoupon.getCId();
        AjaxResult coupon = remoteCouponService.getInfo(cId,"inner");
        ObjectMapper mapper = new ObjectMapper();
        com.ruoyi.business.api.domain.Coupon coupon1 = mapper.convertValue(coupon.get("data"),com.ruoyi.business.api.domain.Coupon.class);
        if(coupon1.getNum() > 1){
            coupon1.setNum(coupon1.getNum() - coupon1.getSize());
            remoteCouponService.edit(coupon1,"inner");
            return userCouponMapper.insertUserCoupon(userCoupon);
        }else {
            System.out.println("数量不足");
            return 0;
        }
    }

    /**
     * 修改用户优惠卷中间
     * 
     * @param userCoupon 用户优惠卷中间
     * @return 结果
     */
    @Override
    public int updateUserCoupon(UserCoupon userCoupon)
    {
        return userCouponMapper.updateUserCoupon(userCoupon);
    }

    /**
     * 批量删除用户优惠卷中间
     * 
     * @param ids 需要删除的用户优惠卷中间主键
     * @return 结果
     */
    @Override
    public int deleteUserCouponByIds(Long[] ids)
    {
        return userCouponMapper.deleteUserCouponByIds(ids);
    }

    /**
     * 删除用户优惠卷中间信息
     * 
     * @param id 用户优惠卷中间主键
     * @return 结果
     */
    @Override
    public int deleteUserCouponById(Long id)
    {
        return userCouponMapper.deleteUserCouponById(id);
    }
}
